package pl.mikei.deliverydroid;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class TimerActivity extends Activity {
	
	private long additionalElapsedTime;
	
	private long startTime;
	
	private boolean isRunning;
	
	private boolean bUseFullScreen = false;
	
	private SharedPreferences sPreferences;
	
	protected void changeFullscreenOLD() {
		Log.i(getString(R.string.ttitle), "changeFullScreen called");
		sPreferences = getPreferences(MODE_PRIVATE);
		Editor edi = sPreferences.edit();
		edi.putBoolean("fullscreen", bUseFullScreen);
		edi.commit();
		
		if (bUseFullScreen) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
		else
		{
			//requestWindowFeature(Window.FEATURE_NO_TITLE);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
	}
	
	protected void changeFullscreen() {
		Log.i(getString(R.string.ttitle), "changeFullScreen called");
		sPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		if (sPreferences.getBoolean("toggle_fullscreen", false)) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
		else
		{
			//requestWindowFeature(Window.FEATURE_NO_TITLE);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		changeFullscreen();
		super.onCreate(savedInstanceState);

		Log.i(getString(R.string.ttitle), "onCreate called");
		
		setContentView(R.layout.timer);

		Thread timer_thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(5000);
						refreshTimer.sendEmptyMessage(0);
					}catch (InterruptedException e) {
					}
				}
			}
		});
		timer_thread.start();

		Button btnStart = (Button)findViewById(R.id.button_start);
		Button btnStop = (Button)findViewById(R.id.button_stop);
		Button btnClear = (Button)findViewById(R.id.button_clear);
		
		btnStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!isRunning) {
					startTime = new Date().getTime();
					isRunning = true;
				}
			}
		}); 
		
		btnStop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				additionalElapsedTime = getElapsedTime();
				isRunning = false;
			}
		});
		
		btnClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isRunning) additionalElapsedTime = 0;
				Log.i(getString(R.string.app_name),"fullscreen: " + bUseFullScreen);
				Log.i(getString(R.string.app_name), String.format("%s", getPreferences(MODE_PRIVATE).getBoolean("fullscreen", false)));
				Log.i(getString(R.string.app_name), String.format("toggle_fullscreen %s", getPreferences(MODE_PRIVATE).getBoolean("toggle_fullscreen", false)));

			}
		});
	}
	
	@Override
	public void onResume() {
		Log.i(getString(R.string.ttitle), "onResume called");
		changeFullscreen();
		super.onResume();
	}
	
	private Handler refreshTimer = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			long elapsed_time = getElapsedTime();
			
			SimpleDateFormat format = new SimpleDateFormat("mm : ss.SSS");
			String current_time = format.format(elapsed_time);
			
			final TextView timer = (TextView)findViewById(R.id.time_label);
			timer.setText(current_time);
			
		}
	};
	
	
	
	public synchronized long getElapsedTime() {
		if (isRunning) {
			return additionalElapsedTime + new Date().getTime() - startTime;
		} else {
			return additionalElapsedTime;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.timer_menu, menu);
		return true;
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i(getString(R.string.ttitle), "Menu selected "+item.getItemId()+ " " + item.getTitle());
		switch (item.getItemId()) {
			case R.id.item_about:
				return super.onOptionsItemSelected(item);
			case R.id.item_preferences:
				startActivity(new Intent(getBaseContext(), PrefsActivity.class));
				return super.onOptionsItemSelected(item);
			case R.id.item_fullscreen:
				changeFullscreen();
				return super.onOptionsItemSelected(item);
			case R.id.item_exit:
				this.finish();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
		
	}
}

